package com.webside.subject.controller;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.mock.MockParameterMetaData.Parameter;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sun.tools.internal.ws.processor.model.Request;
import com.webside.base.basecontroller.BaseController;
import com.webside.dtgrid.model.Pager;
import com.webside.dtgrid.util.ExportUtils;
import com.webside.exception.AjaxException;
import com.webside.exception.ServiceException;
import com.webside.exception.SystemException;
import com.webside.playbackstatistics.service.DictionaryService;
import com.webside.series.model.Bdictionary;
import com.webside.series.service.SeriesService;
import com.webside.subject.model.SubjectEntity;
import com.webside.subject.model.SubjectSeriesEntity;
import com.webside.subject.service.SubjectSeriesService;
import com.webside.subject.service.SubjectService;
import com.webside.util.Common;
import com.webside.util.PageUtil;

@Controller
@Scope("prototype")
@RequestMapping("/subjectSeries/")
public class SubjectSeriesController extends BaseController {
	@Autowired
	private SubjectService subjectService;
	@Autowired
	private SubjectSeriesService subjectseriesService;
	@Autowired
	private SeriesService seriesService;
	@RequestMapping(value="listUI.html", method={RequestMethod.GET,RequestMethod.POST})
	public String listUI(Model model, HttpServletRequest request) {
		try
		{
			String subId =  request.getParameter("subjectId");
			model.addAttribute("subjectId",subId);
			PageUtil page = new PageUtil();
			if(request.getParameterMap().containsKey("page")){
				page.setPageNum(Integer.valueOf(request.getParameter("page")));
				page.setPageSize(Integer.valueOf(request.getParameter("rows")));
				page.setOrderByColumn(request.getParameter("sidx"));
				page.setOrderByType(request.getParameter("sord"));
			}
			model.addAttribute("page", page);
			return Common.BACKGROUND_PATH + "/subject/subjectSeriesList";
		}catch(Exception e)
		{
			throw new SystemException(e);
		}
	}
	/**
	 * ajax分页动态加载模式
	 * @param dtGridPager Pager对象
	 * @throws Exception
	 */
	@RequestMapping(value = "/list.html", method = RequestMethod.POST)
	@ResponseBody
	public Object list(Model model, String gridPager,Bdictionary channel,SubjectSeriesEntity subjectSeriesEntity, HttpServletResponse response,HttpServletRequest request) throws Exception{
		//获取id
		String subId =  request.getParameter("subjectId");
		int subjectId = Integer.parseInt(subId);
		Map<String, Object> parameters = null;
		
		// 映射Pager对象
		Pager pager = JSON.parseObject(gridPager, Pager.class);
		// 判断是否包含自定义参数
		parameters = pager.getParameters();
		if (parameters.size() < 0) {
			parameters.put("name", null);
		}
		//3、判断是否是导出操作
		if(pager.getIsExport())
		{
			if(pager.getExportAllData())
			{
				//3.1、导出全部数据
				List<SubjectSeriesEntity> list = subjectseriesService.querySubjectListByPage(parameters);
				ExportUtils.exportAll(response, pager, list);
				return null;
			}else
			{
				//3.2、导出当前页数据
				ExportUtils.export(response, pager);
				return null;
			}
		}else
		{
			
			// 设置分页，page里面包含了分页信息
			Page<Object> page = PageHelper.startPage(pager.getNowPage(),pager.getPageSize());
			parameters.put("subjectId", subjectId);
			List<SubjectSeriesEntity> list = subjectseriesService.querySubjectListByPage(parameters);
			 
			parameters.clear();
			parameters.put("isSuccess", Boolean.TRUE);
			parameters.put("nowPage", pager.getNowPage());
			parameters.put("pageSize", pager.getPageSize());
			parameters.put("pageCount", page.getPages());
			parameters.put("recordCount", page.getTotal());
			parameters.put("startRecord", page.getStartRow());
			parameters.put("exhibitDatas", list);
			return parameters;
		}
		
	}
	

}
